package com.java.controller;

import com.alibaba.fastjson.JSONObject;
import com.java.sevice.ITransferService;
import com.java.utils.HttpParamUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@RestController
@RequestMapping(value = "/transfer")
@Api(tags = "")
public class TransferController {
    @Autowired
    private ITransferService transferService;

    /**
     * 转账锁定接口
     */
    @PostMapping(value = "/tryTransfer")
    public Object tryTransfer(@RequestBody String param) throws Exception {
        JSONObject json = HttpParamUtil.paramToJson(param);
        Long accountId = json.getLong("accountId");
        BigDecimal amount = json.getBigDecimal("amount");
        return this.transferService.tryTransfer(accountId, amount);
    }

    /**
     * 转账确认接口
     */
    @PostMapping(value = "/confirmTransfer")
    public Object confirmTransfer(@RequestBody String param) throws Exception {
        JSONObject json = HttpParamUtil.paramToJson(param);
        Long accountId = json.getLong("accountId");
        BigDecimal amount = json.getBigDecimal("amount");
        return this.transferService.confirmTransfer(accountId, amount);
    }

    /**
     * 转账取消接口
     */
    @PostMapping(value = "/cancelTransfer")
    public Object cancelTransfer(@RequestBody String param) throws Exception {
        JSONObject json = HttpParamUtil.paramToJson(param);
        Long accountId = json.getLong("accountId");
        BigDecimal amount = json.getBigDecimal("amount");
        return this.transferService.cancelTransfer(accountId, amount);
    }
}
